package com.genius.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.genius.annotation.Log;
import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.enums.BusinessType;
import com.genius.enums.ResponseType;
import com.genius.service.DeptService;
import com.genius.system.bo.DeleteDeptByIdBO;
import com.genius.system.bo.InsertDeptBO;
import com.genius.system.bo.SelectDeptBO;
import com.genius.system.bo.UpdateDeptBO;
import com.genius.system.pojo.Dept;
import com.genius.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "机构管理接口", tags = {"用于机构管理的接口"})
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/selectAllDept")
    @ApiOperation(value = "查询机构",notes = "查询机构", httpMethod = "POST")
    public JSONResult selectAllDept(@Valid @RequestBody SelectDeptBO selectDeptBO) {
        Dept dept = JSONUtil.parse(selectDeptBO).toBean(Dept.class);
        List<Dept> list = deptService.selectAllDept(dept);
        return JSONResult.ok(list);
    }

    @PostMapping("/insertDept")
    @ApiOperation(value = "添加机构",notes = "添加机构", httpMethod = "POST")
    @PreFuncAuth("SYS:DEPT:INSERT")
    public JSONResult insertDept(@Valid @RequestBody InsertDeptBO insertDeptBO) {
        Dept dept = JSONUtil.parse(insertDeptBO).toBean(Dept.class);
        Long deptId = IdUtil.getSnowflake().nextId();
        dept.setDeptId(deptId);
        dept.setCreateBy(SecurityUtils.getUsername());
        int rows = deptService.insertDept(dept);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/updateDept")
    @ApiOperation(value = "修改机构",notes = "修改机构", httpMethod = "POST")
    @PreFuncAuth("SYS:DEPT:UPDATE")
    public JSONResult updateDept(@Valid @RequestBody UpdateDeptBO updateDeptBO) {
        Dept dept = JSONUtil.parse(updateDeptBO).toBean(Dept.class);
        dept.setUpdateBy(SecurityUtils.getUsername());
        int rows = deptService.updateDept(dept);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteDeptById")
    @ApiOperation(value = "删除机构",notes = "删除机构", httpMethod = "POST")
    @PreFuncAuth("SYS:DEPT:DELETE")
    @Log(title = "删除机构", businessType = BusinessType.DELETE)
    public JSONResult deleteDeptById(@Valid @RequestBody DeleteDeptByIdBO deleteDeptByIdBO) {
        if (deptService.hasChildByDeptId(deleteDeptByIdBO.getDeptId())) {
            return JSONResult.errorCustom(ResponseType.CHILD_NODE_EXIST_ERROR);
        }
        int rows = deptService.deleteDeptById(deleteDeptByIdBO.getDeptId());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
